//
//  NewStoreImageScrollView.m
//  Yanwei
//
//  Created by weijunjie on 2018/7/25.
//  Copyright © 2018年 DCloud. All rights reserved.
//

#import "NewStoreImageScrollView.h"
#import "CLPlayerMaskView.h"
#import "UIView+Util.h"
#import "NewStoreImageCoverView.h"
#import "CLPlayerView.h"
#import "NewStoreRemarkView.h"

@interface NewStoreImageScrollView ()
 <UITableViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *currentPageLabel;
@property (nonatomic, assign) NSUInteger maxCount;
@property (nonatomic, strong) UIView *coverView;
@end
@implementation NewStoreImageScrollView

- (instancetype)initWithFrame:(CGRect)frame videoPath:(NSString *)videoPath {
    if (self = [super initWithFrame:frame]) {
        _videoPath = videoPath;
        self.agreePhoneNet = NO;
    }
    return self;
}

- (void)setImageURLStringsGroup:(NSArray *)imageURLStringsGroup
{
    _imageURLStringsGroup = imageURLStringsGroup;
    
    NSMutableArray *temp = [NSMutableArray new];
    [_imageURLStringsGroup enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * stop) {
        NSString *urlString;
        if ([obj isKindOfClass:[NSString class]]) {
            urlString = obj;
        } else if ([obj isKindOfClass:[NSURL class]]) {
            NSURL *url = (NSURL *)obj;
            urlString = [url absoluteString];
        }
        if (urlString) {
            [temp addObject:urlString];
        }
    }];
    
    CGFloat WH = self.width;
    NSUInteger count = temp.count;
    __weakConfigure
    for (int i = 0; i < count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [self.scrollView addSubview:imageView];
        imageView.tag = i;
        if (i == 0 && self.videoPath.length != 0) { // 有视频
//        if (i == 0) { // 有视频
            UIImageView *iconView = [[UIImageView alloc] init];
            iconView.image = [UIImage imageNamed:@"playVideo"];
            [imageView addSubview:iconView];
            [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.mas_equalTo(58);
                make.center.equalTo(imageView);
            }];
            
            // 遮罩
            [imageView addSubview:self.coverView];
            [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(imageView);
            }];
            
            NewStoreImageCoverView *imageCoverView = [[NewStoreImageCoverView alloc] init];
            [self.coverView addSubview:imageCoverView];
            imageCoverView.cancleActionBlock = ^{
                __weakSelf.coverView.hidden = YES;
            };
            imageCoverView.goOnActionBlock = ^{
                __weakSelf.coverView.hidden = YES;
                __weakSelf.agreePhoneNet = YES;
                // 播放
                if (__weakSelf.clickItemOperationBlock) {
                    __weakSelf.clickItemOperationBlock(imageView,__weakSelf.currentPageLabel,__weakSelf);
                }
            };
            CGFloat titleH = [@"播放" boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : APPFONT(14)} context:nil].size.height;
            [imageCoverView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(275);
                make.height.mas_equalTo(titleH + 33 + 74);
                make.center.equalTo(self.coverView);
            }];
            
//            if ([[DeviceDataInfo currentNetWork] isEqualToString:@"1"]) { // wifi,自动播放
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    if (__weakSelf.clickItemOperationBlock) {
//                        __weakSelf.clickItemOperationBlock(imageView,__weakSelf.currentPageLabel,__weakSelf);
//                    }
//                });
//            }
        }
        imageView.frame = CGRectMake(i * WH, 0, WH, WH);
        [imageView sd_setImageWithURL:[NSURL URLWithString:temp[i]] placeholderImage:NewStoreDefault];
        [imageView tapUpWithBlock:^(UIView *v) {
            if (__weakSelf.clickItemOperationBlock) {
                __weakSelf.clickItemOperationBlock(v,__weakSelf.currentPageLabel,__weakSelf);
            }
        }];

    }
    
    self.scrollView.contentSize = CGSizeMake(WH * count, 0);
    self.currentPageLabel.text = [NSString stringWithFormat:@"1/%zd",count];
    self.maxCount = count;
    
}


/**
 自动播放
 */
- (void)autoPlay {
    
}

#pragma mark - 懒加载
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scrollView];
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        UIView *bottomLineView = [[UIView alloc] init];
        bottomLineView.backgroundColor = Color217;
        [self addSubview:bottomLineView];
        [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.mas_equalTo(0.5);
            
        }];
    }
    return _scrollView;
}

- (UILabel *)currentPageLabel {
    if (!_currentPageLabel) {
        _currentPageLabel = [[UILabel alloc] init];
        _currentPageLabel.font = APPFONT(9);
        _currentPageLabel.textColor = [UIColor whiteColor];
        _currentPageLabel.backgroundColor = RGBACOLOR(140, 140, 140, 0.7);
        _currentPageLabel.textAlignment = NSTextAlignmentCenter;
        _currentPageLabel.layer.cornerRadius = 10;
        _currentPageLabel.layer.masksToBounds = YES;
        [self addSubview:_currentPageLabel];
        [_currentPageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-15);
            make.bottom.equalTo(self).offset(-10);
            make.width.mas_equalTo(36);
            make.height.mas_equalTo(20);
        }];
    }
    return _currentPageLabel;
}

- (UIView *)coverView {
    if (!_coverView) {
        _coverView = [[UIView alloc] init];
        _coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        _coverView.hidden = YES;
    }
    return _coverView;
}

- (void)setCoverHidden:(BOOL)coverHidden {
    self.coverView.hidden = coverHidden;
}


#pragma mark - UITableViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
     CGFloat offsetX = self.scrollView.contentOffset.x;
    if (offsetX < 0) return;
    int currentPage = (offsetX / scrollView.width) + 1.5;
    if (currentPage > self.maxCount) return;
    if (currentPage == 1 && self.maskView != nil) {
        if (self.maskView.bottomToolBar.alpha <= 0) {
            self.currentPageLabel.alpha = 1.0;
        } else {
            self.currentPageLabel.alpha = 0;
        }
        CLPlayerView *playView = (CLPlayerView *)self.maskView.superview;
        if (self.maskView.slider.value > 0 && self.maskView.slider.value < 1 &&  [playView isKindOfClass:[CLPlayerView class]]) {
            [playView playVideo];
        }
        self.storeRemarkView.hidden = YES;
    } else {
        self.currentPageLabel.alpha = 1.0;
        CLPlayerView *playView = (CLPlayerView *)self.maskView.superview;
        if ([playView isKindOfClass:[CLPlayerView class]]) {
            [playView pausePlay];
        }

        self.storeRemarkView.hidden = NO;
        if (self.storeRemarkView != nil && self.maskView == nil) {
            self.storeRemarkView = nil;
        }
    }
    
    self.currentPageLabel.text = [NSString stringWithFormat:@"%d/%zd",currentPage,self.maxCount];
}

@end
